package com.example.myanimation;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AnimationSet;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.animation.ValueAnimator.*;

public class AnimationSetActivity extends AppCompatActivity {

    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.spinner)
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation_set);
        ButterKnife.bind(this);
        initEvent();
    }

    private void initEvent() {

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1:
                        Sequentially();
                        break;
                    case 2:
                        Together();
                        break;
                    case 3:
                        AnimatorSetBuilder();
                        break;
                    case 4:
                        AnimatorDelay();
                        break;
                    case 5:
                        AnimatorTag();
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });
    }

    private void AnimatorTag() {
        /**
         *
         * 因为AnimatorSet设置了target，所以所有的动画都会在bt1身上执行
         */
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(bt2, "translationY",  0f, 200f, 400f, 0f);
        ObjectAnimator anim1 = ObjectAnimator.ofInt(bt1, "BackgroundColor",  0xffff00ff, 0xffffff00, 0xffff00ff);
        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(anim1, anim3);

        animSet.setDuration(3000);
        animSet.setTarget(bt1);
        animSet.start();
    }

    private void AnimatorDelay() {
        /**
         * 动画延时
         *  animSet.play(anim2).with(anim3);
            animSet.setStartDelay(2000);
         *
         * 1.anim2、anim3没有单独设置延时，animSet设置延时，
         *      所有的动画都是在set延时之后运动
         * 2.anim3、animSet设置延时设置延时，anim2没有设置延时
         *       所有的动画都是在set延时之后运动，之后anim2动画，延时之后anim3动画
         * 3.animSet、anim2设置延时设置延时，anim3没有设置延时
         *       动画在set的延时时间+anim2延时时间之后运动
         *
         *结论：
         *      AnimatorSet的延时是仅针对性的延长AnimatorSet激活时间的，对单个动画的延时设置没有影响。
         *      AnimatorSet真正激活延时 = AnimatorSet.startDelay+第一个动画.startDelay
         *      在AnimatorSet激活之后，第一个动画绝对是会开始运行的，后面的动画则根据自己是否延时自行处理。
         *      注意。说的是第一个动画，那就是after
         *
         *
         */

        ObjectAnimator anim2 = ObjectAnimator.ofFloat(bt1, "translationY",  0f, 200f, 400f, 0f);
        anim2.setStartDelay(2000);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(bt2, "translationY",  0f, 200f, 400f, 0f);
        AnimatorSet animSet = new AnimatorSet();
        /**
         * 所有的动画都是延长2秒之后运行，和
         */
        animSet.play(anim2).after(anim3);
        animSet.setStartDelay(2000);
        animSet.setDuration(3000);
        animSet.start();

    }

    private void AnimatorSetBuilder() {
        /**
         * 自定义动画顺序
         *  无论是with，befor，after都是相对于play的
         *
         *  with：和play一起运动
         *  befor：play动画结束之后动画
         *  after：play动画之前动画
         *
         */

        ObjectAnimator anim1 = ObjectAnimator.ofInt(bt1, "BackgroundColor",  0xffff00ff, 0xffffff00, 0xffff00ff);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(bt1, "rotation",0f,20f,-20,40,-40,60,-60,0f);

        ObjectAnimator anim3 = ObjectAnimator.ofFloat(bt2, "scaleX",2f);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(bt2, "scaleY",2f);
        ObjectAnimator anim5 = ObjectAnimator.ofFloat(bt2, "alpha", 0f, 0.2f,0.8f ,1);


        AnimatorSet set = new AnimatorSet();
        AnimatorSet.Builder builder = set.play(anim1);
        // anim2和anim1一起运行
        //在anim5之后运行anim1和anim2，，在anim4和anim3之前运行anim1和anim2

        builder.with(anim2).before(anim4).after(anim5).before(anim3);

        set.setDuration(3000);
        set.start();



    }

    private void Together() {
        /**
         * 这是共同执行的动画，多个动画一起执行，
         */
        ObjectAnimator anim1 = ObjectAnimator.ofInt(bt1, "BackgroundColor",  0xffff00ff, 0xffffff00, 0xffff00ff);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(bt1, "translationY",  0f, 200f, 400f, 0f);

        anim2.setStartDelay(2000);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(bt2, "translationY",  0f, 200f, 400f, 0f);
        anim3.setRepeatCount(INFINITE);// 无限循环

        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(anim1, anim2, anim3);
        animSet.setDuration(3000);
        animSet.start();

    }

    private void Sequentially() {
        /**
         * 这是队列动画，只有第一个动画结束之后第二个才能开始，如果第一个无限循环，那么第二个动画也不会执行
         */
        ObjectAnimator anim1 = ObjectAnimator.ofInt(bt1, "BackgroundColor",  0xffff00ff, 0xffffff00, 0xffff00ff);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(bt1, "translationY",  0f, 200f, 400f, 0f);
//        anim2.setRepeatCount(INFINITE);// 无限循环
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(bt2, "translationY",  0f, 200f, 400f, 0f);

        AnimatorSet animSet = new AnimatorSet();
        animSet.playSequentially(anim1, anim2, anim3);
        animSet.setDuration(3000);
        animSet.start();

    }

}
